package api

import (
	"day12_30/internal/api/request"
	"day12_30/internal/api/response"
	"day12_30/internal/models/service"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func UserRegister(c *gin.Context) {
	var json request.UserRegisterRequest
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	register, s := service.UserRegister(&json)
	if s != "" {
		response.Response(c, 400, s, nil)
		return
	}

	response.Response(c, 200, "注册成功", register)

}

func UserLogin(c *gin.Context) {
	var json request.UserLoginRequest
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	login, s := service.UserLogin(&json)
	if s != "" {
		response.Response(c, 400, s, nil)
		return
	}

	find := service.UserFind(&json, login)

	response.Response(c, 200, "登录成功", find)

}

func SendMessage(c *gin.Context) {
	var json request.SendMessage
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	message := service.SendMessage(&json)

	if message != "" {
		response.Response(c, 400, message, nil)
		return
	}

	log.Println("短信发送成功")
	response.Response(c, 200, "短信发送成功", nil)

}
